<!DOCTYPE html>
<html>
<head>
    <title>路由数据</title>
    <style>
        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 2rem;
        }

        .section-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 2rem;
        }

        .table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 1rem;
        }

        .table th,
        .table td {
            padding: 0.75rem;
            border: 1px solid #dee2e6;
        }

        .table th {
            background-color: #f8f9fa;
            font-weight: 600;
        }

        .actions {
            display: flex;
            gap: 0.5rem;
        }

        .btn {
            display: inline-block;
            padding: 0.5rem 1rem;
            border-radius: 4px;
            text-decoration: none;
            cursor: pointer;
            border: none;
            font-size: 1rem;
        }

        .btn-primary {
            background-color: #007bff;
            color: white;
        }

        .btn-danger {
            background-color: #dc3545;
            color: white;
        }

        .btn-sm {
            padding: 0.25rem 0.5rem;
            font-size: 0.875rem;
        }

        .alert {
            padding: 1rem;
            margin-bottom: 1rem;
            border-radius: 4px;
        }

        .alert-success {
            background-color: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }

        .alert-danger {
            background-color: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }

        .alert-info {
            background-color: #d1ecf1;
            color: #0c5460;
            border: 1px solid #bee5eb;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="section-header">
            <h1>路由数据</h1>
            <a href="/db/routes/add" class="btn btn-primary">添加路由</a>
        </div>
        
        <div id="routeList"></div>
    </div>

    <script>
        // 获取路由列表
        async function fetchRouteList() {
            try {
                const response = await fetch('/api/routes');
                const data = await response.json();
                if (data.status === 'success') {
                    displayRouteList(data.data);
                } else {
                    showError('获取路由列表失败');
                }
            } catch (error) {
                console.error('获取路由列表失败:', error);
                showError('获取路由列表失败');
            }
        }

        // 显示路由列表
        function displayRouteList(routes) {
            const routeList = document.getElementById('routeList');
            if (routes.length === 0) {
                routeList.innerHTML = '<div class="alert alert-info">当前无主机调动状态</div>';
                return;
            }

            const table = document.createElement('table');
            table.className = 'table';
            
            // 表头
            const thead = document.createElement('thead');
            thead.innerHTML = `
                <tr>
                    <th>模块ID</th>
                    <th>子模块ID(cmdid)</th>
                    <th>服务器IP</th>
                    <th>服务器端口</th>
                    <th>操作</th>
                </tr>
            `;
            table.appendChild(thead);
            
            // 表格内容
            const tbody = document.createElement('tbody');
            routes.forEach(route => {
                const tr = document.createElement('tr');
                tr.innerHTML = `
                    <td>${route.modid}</td>
                    <td>${route.cmdid}</td>
                    <td>${route.serverip}</td>
                    <td>${route.serverport}</td>
                    <td class="actions">
                        <a href="/db/routes/edit?id=${route.id}" class="btn btn-sm btn-primary">编辑</a>
                        <button onclick="deleteRoute(${route.id})" class="btn btn-sm btn-danger">删除</button>
                    </td>
                `;
                tbody.appendChild(tr);
            });
            table.appendChild(tbody);
            
            routeList.innerHTML = '';
            routeList.appendChild(table);
        }

        // 删除路由
        async function deleteRoute(id) {
            if (confirm('确定要删除这个路由吗？')) {
                try {
                    const response = await fetch(`/api/routes/${id}`, {
                        method: 'DELETE'
                    });
                    if (response.ok) {
                        showSuccess('删除成功');
                        fetchRouteList(); // 刷新列表
                    } else {
                        showError('删除失败');
                    }
                } catch (error) {
                    console.error('删除路由失败:', error);
                    showError('删除失败');
                }
            }
        }

        // 显示成功消息
        function showSuccess(message) {
            const alert = document.createElement('div');
            alert.className = 'alert alert-success';
            alert.textContent = message;
            document.querySelector('.container').insertBefore(alert, document.getElementById('routeList'));
            setTimeout(() => alert.remove(), 3000);
        }

        // 显示错误消息
        function showError(message) {
            const alert = document.createElement('div');
            alert.className = 'alert alert-danger';
            alert.textContent = message;
            document.querySelector('.container').insertBefore(alert, document.getElementById('routeList'));
            setTimeout(() => alert.remove(), 3000);
        }

        // 页面加载时获取路由列表
        document.addEventListener('DOMContentLoaded', fetchRouteList);
    </script>
</body>
</html>
